﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;



namespace School.Pages.Admission
{
    public partial class TestSubject : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1018) == true)
                {
                    if (!(Page.IsPostBack))
                    {
                        //security

                        //fill crls
                        Fillcrls();
                        InitializeControls();
                        GridDataBind();

                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();
            if (ddlSearchTest_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Test_Id", ddlSearchTest_Id.SelectedValue.ToString());
            }
            if (txtSearchName.Text.Length > 0)
            {
                SearchTable.Add("Name", txtSearchName.Text.ToString());
            }
            if (txtSearchDuration.Text.Length > 0)
            {
                SearchTable.Add("Duration", txtSearchDuration.Text.ToString());
            }

            IEnumerable<ADM_Test_Subject> result = Test_Subject.All(SearchTable);
            
            griddata.DataSource = result ;

            griddata.DataBind();

            griddata.Visible = true;
            mycount = result.Count();
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {

            ddlcampus_Id.DataSource = Structure.GetCampuses();
            ddlcampus_Id.DataTextField = "Name";
            ddlcampus_Id.DataValueField = "Id";
            ddlcampus_Id.DataBind();
            ddlcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlCertificate_Id.DataSource = Structure.GetCertificates();
            ddlCertificate_Id.DataTextField = "Name";
            ddlCertificate_Id.DataValueField = "Id";
            ddlCertificate_Id.DataBind();
            ddlCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchCertificate_Id.DataSource = Structure.GetCertificates();
            ddlSearchCertificate_Id.DataTextField = "Name";
            ddlSearchCertificate_Id.DataValueField = "Id";
            ddlSearchCertificate_Id.DataBind();
            ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchGrade_ID.DataSource = Structure.GetGrades();
            ddlSearchGrade_ID.DataTextField = "Name";
            ddlSearchGrade_ID.DataValueField = "id";
            ddlSearchGrade_ID.DataBind();
            ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));


            ddlGrade_Id.DataSource = Structure.GetGrades();
            ddlGrade_Id.DataTextField = "Name";
            ddlGrade_Id.DataValueField = "id";
            ddlGrade_Id.DataBind();
            ddlGrade_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                
            ddlSearchStage_Id.DataSource = Structure.GetGrades();
            ddlSearchStage_Id.DataTextField = "Name";
            ddlSearchStage_Id.DataValueField = "id";
            ddlSearchStage_Id.DataBind();
            ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlStage_Id.DataSource = Structure.GetGrades();
            ddlStage_Id.DataTextField = "Name";
            ddlStage_Id.DataValueField = "id";
            ddlStage_Id.DataBind();
            ddlStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlEductionalYear_Id.DataSource = Structure.GetCurrentEductionalYear();
            ddlEductionalYear_Id.DataTextField = "Name";
            ddlEductionalYear_Id.DataValueField = "id";
            ddlEductionalYear_Id.DataBind();

            ddlSearchEductionalYear_Id.DataSource = Structure.GetCurrentEductionalYear();
            ddlSearchEductionalYear_Id.DataTextField = "Name";
            ddlSearchEductionalYear_Id.DataValueField = "id";
            ddlSearchEductionalYear_Id.DataBind();
            ddlSearchEductionalYear_Id.Items.Insert(0, new ListItem("اختيار", "-1"));


            ddlSearchTest_Id.DataSource = ADMTest.All().Select(x => new { Date = x.FromDate.ToShortDateString() + " " + x.ORG_Campus.Name.ToString() + "-" + x.ORG_Certificate.Name.ToString() + "-" + x.ORG_Stage.Name.ToString() + "-" + x.ORG_Grade.Name.ToString(), id = x.Id });
            ddlSearchTest_Id.DataTextField = "Date";
            ddlSearchTest_Id.DataValueField = "id";
            ddlSearchTest_Id.DataBind();
            ddlSearchTest_Id.Items.Insert(0, new ListItem("اختيار", "-1"));


            ddlTest_Id.DataSource = ADMTest.All().Select(x => new { Date = x.FromDate.ToShortDateString() + " " + x.ORG_Campus.Name.ToString() + "-" + x.ORG_Certificate.Name.ToString() + "-" + x.ORG_Stage.Name.ToString() + "-" + x.ORG_Grade.Name.ToString(), id = x.Id });
            ddlTest_Id.DataTextField = "Date";
            ddlTest_Id.DataValueField = "id";
            ddlTest_Id.DataBind();
            ddlTest_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            txtName.Text = "";
            txtDuration.Text = "";

        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
           
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;

        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long CID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = Test_Subject.Delete(CID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "تم حذف البيانات بنجاح";

                }
            }
            catch (Exception)
            {
                lblMessage.Text = "  لم يتم حذف البيانات بنجاح رجاء مراجعة مدير النظام";

            }

        }


        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to delete record(s) (" +
                DataBinder.Eval(e.Row.DataItem, "Name") + ") ?')");

            }
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;
            pnldetails.Visible = true;
            DetailsView1bind();
        }

        protected void DetailsView1bind()
        {
            Hashtable SearchTable = new Hashtable();

                SearchTable.Add("Id", griddata.SelectedDataKey.Value.ToString());

            
            DetViewdata.DataSource = Test_Subject.All(SearchTable);
            DetViewdata.DataBind();
        }



        # endregion



        # region   "Execute commands sent to the form"



        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
            pnlbrows.Visible = false;
            pnldetails.Visible = false;
        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = true;
            GridDataBind();
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
            Int64 Test_Id = Convert.ToInt64(ddlTest_Id.SelectedValue);
            Int32 duration = Convert.ToInt32(txtDuration.Text);
            Int64 xx = Test_Subject.Add(new ADM_Test_Subject() { Test_Id = Test_Id, Name = txtName.Text, Duration = duration });
            if (xx != -1)
            {
                lblMessageAdd.Text = "تم حفظ البيانات بنجاح";
                InitializeControls();
            }
            else
            {
                lblMessageAdd.Text = "لم يتم حفظ البيانات";
            }
            
        }


        # endregion



        # region " Search *********************"




        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();

            IEnumerable<ADM_Test_Subject> result = Test_Subject.All(SearchTable);

            griddata.DataSource = result;

            griddata.DataBind();

            griddata.Visible = true;
            mycount = result.Count();
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }
        }


        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void ddlCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlGrade_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txtShowCampus_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            
        }

        protected void ddlTest_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ADM_Test test= ADMTest.Single(Convert.ToInt64(ddlTest_Id.SelectedValue));
            if (test != null) {
                ddlcampus_Id.SelectedIndex = ddlcampus_Id.Items.IndexOf(ddlcampus_Id.Items.FindByValue(test.Campus_Id.ToString()));
                ddlCertificate_Id.SelectedIndex = ddlCertificate_Id.Items.IndexOf(ddlCertificate_Id.Items.FindByValue(test.Certificate_Id.ToString()));
                ddlStage_Id.SelectedIndex = ddlStage_Id.Items.IndexOf(ddlStage_Id.Items.FindByValue(test.Stage_Id.ToString()));
                ddlGrade_Id.SelectedIndex = ddlGrade_Id.Items.IndexOf(ddlGrade_Id.Items.FindByValue(test.Grade_Id.ToString()));

            }
            
        }

        protected void ddlSearchTest_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ADM_Test test = ADMTest.Single(Convert.ToInt64(ddlSearchTest_Id.SelectedValue));
            if (test != null)
            {
                ddlSearchcampus_Id.SelectedIndex = ddlSearchcampus_Id.Items.IndexOf(ddlSearchcampus_Id.Items.FindByValue(test.Campus_Id.ToString()));
                ddlSearchCertificate_Id.SelectedIndex = ddlSearchCertificate_Id.Items.IndexOf(ddlSearchCertificate_Id.Items.FindByValue(test.Certificate_Id.ToString()));
                ddlSearchStage_Id.SelectedIndex = ddlSearchStage_Id.Items.IndexOf(ddlSearchStage_Id.Items.FindByValue(test.Stage_Id.ToString()));
                ddlSearchGrade_ID.SelectedIndex = ddlSearchGrade_ID.Items.IndexOf(ddlSearchGrade_ID.Items.FindByValue(test.Grade_Id.ToString()));

            }
            
        }


        

        


    }
}